﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using System.Text;
using System.Collections;
using Actuals.Data;

namespace Actuals.Data
{
    public class DalTeamMember : Dal<ITeamMember, TeamMember>, IDalTeamMember
    {
           private readonly IDalManager _manager;
        private static readonly IDalTeamMember _instance = new DalTeamMember();
        private const string SP_GET_TeamMemberByTeam = "usp_GetTeamMemberByTeam";
        private const string SP_GET_Emp_TeamMember = "usp_GetEmployeeTeamMember";
        private const string SP_GET_TeamMember = "usp_GetTeamMember";
        private const string SP_ADD_TEAMMEMBER = "usp_AddTeamMember";
        private const string SP_UPDATE_TEAMMEMBER = "usp_UpdateTeamMember";
        private const string SP_DELETE_TEAMMEMBER = "usp_DeleteTeamMember";


        


        public static IDalTeamMember Instance
        {
            get { return _instance; }
        }

        public DalTeamMember()
        {
            _manager = new DalManager();
        }

        public DalTeamMember(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(ITeamMember obj, DataRow row)
        {
            obj.Id = _manager.GetValue<Int64>(row, "Id");
            obj.TeamId = _manager.GetValue<int>(row, "TeamId");
            obj.EmployeeId = _manager.GetValue<int>(row, "EmployeeId");
            obj.NoOfHoursPerDay = _manager.GetValue<int>(row, "NoOfHoursPerDay");
            obj.WorkMode = _manager.GetValue<string>(row, "WorkMode");
        }

        #region IDalProject Members

        public IList<ITeamMember> GetTeamMembers(int teamId)
        {
            _manager.StoredProcedureName = SP_GET_TeamMemberByTeam;
            _manager.AddInParameter("@TeamId", DbType.Int32, teamId);
            
                    
           
             IList<ITeamMember> teamMembers  = new List<ITeamMember>();
            foreach (ITeamMember teamMember in ConvertAll(_manager.ExecuteDataSet()))
                teamMembers.Add(teamMember );

            return teamMembers;

               
        }

        public IList<ITeamMember> GetTeamMemberByEmployee(int employeeId)
        {
            _manager.StoredProcedureName = SP_GET_Emp_TeamMember;
            _manager.AddInParameter("@EmployeeId", DbType.Int32, employeeId);

            IList<ITeamMember> teamMembers = new List<ITeamMember>(); 
            foreach (ITeamMember teamMember in ConvertAll(_manager.ExecuteDataSet()))
                teamMembers.Add(teamMember);
            return teamMembers;


        }

        public ITeamMember GetTeamMember(Int64 teamMemberId)
        {
            _manager.StoredProcedureName = SP_GET_TeamMember;
            _manager.AddInParameter("@TeamMemberId", DbType.Int64, teamMemberId);


            ITeamMember teamMember = new TeamMember();
            Convert(teamMember, _manager.ExecuteDataSet());
            return teamMember;


        }
        public int AddTeamMember(ITeamMember teamMember)
        {

            _manager.StoredProcedureName = SP_ADD_TEAMMEMBER;
            _manager.AddInParameter("@TeamId",DbType.Int32, teamMember.TeamId);
            _manager.AddInParameter("@WorkMode",DbType.String, teamMember.WorkMode);
            _manager.AddInParameter("@EmployeeId",DbType.Int32, teamMember.EmployeeId);
            _manager.AddInParameter("@NoOfHoursPerDay",DbType.Int32, teamMember.NoOfHoursPerDay);
            return _manager.ExecuteNonQuery();
                       
        }

        public int UpdateTeamMember(ITeamMember teamMember)
        {

            _manager.StoredProcedureName = SP_UPDATE_TEAMMEMBER;
            _manager.AddInParameter("@TeamMemberId", DbType.Int32, teamMember.Id);
            _manager.AddInParameter("@TeamId", DbType.Int32, teamMember.TeamId);
            _manager.AddInParameter("@WorkMode", DbType.String, teamMember.WorkMode);
            _manager.AddInParameter("@EmployeeId", DbType.Int32, teamMember.EmployeeId);
            _manager.AddInParameter("@NoOfHoursPerDay", DbType.Int32, teamMember.NoOfHoursPerDay);
            return _manager.ExecuteNonQuery();

        }
        public int DeleteTeamMember(Int64 teamMemberId)
        {

            _manager.StoredProcedureName = SP_DELETE_TEAMMEMBER;
            _manager.AddInParameter("@TeamMemberId", DbType.Int64, teamMemberId);            
            return _manager.ExecuteNonQuery();

        }



        #endregion


        
    }
}
